<template>
  <div class="container-page-address cont">
    <div v-for="(temp,index) in list" :key="index" class="group-list-card">
      <itemInfoAddress :value="temp" />
      <div class="group-card-btm">
        <van-radio-group v-model="radio">
          <van-radio :name="index" checked-color="#D63A54" @click="settingDefault(temp)">设为默认</van-radio>
        </van-radio-group>
        <div class="group-btn">
          <div class="item-btn" @click="routerLink(`addAddress`, temp)"><img class="item-icon" src="@/assets/change.png">编辑</div>
          <div class="item-btn" @click="delAddress(temp.id)"><img class="item-icon" src="@/assets/delete.png">删除</div>
        </div>
      </div>
    </div>
    <div class="item-btn-add" @click="routerLink(`addAddress`)">＋新增收货地址</div>
  </div>
</template>

<script>
// api
import { userAddressList, userAddressDel, userAddressEdit } from '@/api/miva'
// item
import itemInfoAddress from '@/components/miva-item/item-info-address'
export default {
  components: { itemInfoAddress },
  data() {
    return {
      radio: 0,
      list: []
    }
  },
  async mounted() {
    // 获取地址列表
    await this.getAddressList()
  },
  methods: {
    // 获取地址列表
    async getAddressList() {
      const res = await userAddressList()
      this.list = res.data
    },
    // 删除收货地址
    async delAddress(id) {
      await userAddressDel({ id })
      // 重新获取地址列表
      await this.getAddressList()
    },
    // 设置默认地址
    async settingDefault(data) {
      data.is_default = 0
      await userAddressEdit({ id: data.id, data })
      // 重新获取地址列表
      await this.getAddressList()
      this.radio = 0
    },
    routerLink(name, params = {}) {
      this.$router.push({ name, params })
    }
  }
}
</script>

<style lang="less">
.container-page-address {
  padding: 10px;
  padding-bottom: 50px;
  .group {
    &-list-card {
      padding: 10px;
      margin-bottom: 10px;
      border-radius: 6px;
      background-color: #ffffff;
    }
    &-card-btm {
      padding: 10px 0 0;
      display: flex;
      justify-content: space-between;
      border-top: 1px solid #ddd;
    }
    &-btn {
      display: flex;
      align-items: center;
    }
  }
  .item {
    &-btn {
      margin-left: 10px;
    }
    &-icon {
      width: 15px;
      height: 15px;
      margin-right: 5px;
      position: relative;
      top: 2px;
    }
    &-btn-add {
      width: calc(100% - 20px);
      padding: 10px;
      position: fixed;
      bottom: 10px;
      left: 50%;
      transform: translateX(-50%);
      border-radius: 10px;
      font-size: 14px;
      text-align: center;
      background: #d63a54;
      color: #fff;
    }
  }
}
</style>
